package com.winter.boot.config;

import java.util.HashMap;
import java.util.Map;

public class DefaultBeanPostProcessor implements BeanPostProcessor {

    private final ThreadLocal<Map<String, Thread>> threadThreadLocal = new ThreadLocal<>();

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        Map<String, Thread> threadMap = threadThreadLocal.get();
        for (String key : threadMap.keySet()) {
            System.out.println("result: " + bean + ": after(), Thread:" + threadMap.get(key));
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        Thread thread = Thread.currentThread();//获取当前执行线程
        Map<String, Thread> map = new HashMap<>(1);
        map.put(thread.getId() + thread.getName(), thread);
        threadThreadLocal.set(map);
        System.out.println("log:" + beanName + ": before()" + ", Thread:" + thread.getName());
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }
}
